package cn.ava.ymcc.web.controller;

import cn.ava.ymcc.dto.KillCourseOrderParamDto;
import cn.ava.ymcc.service.IKillCourseService;
import cn.ava.ymcc.domain.KillCourse;
import cn.ava.ymcc.query.KillCourseQuery;
import cn.ava.ymcc.result.JSONResult;
import cn.ava.ymcc.result.PageList;
import cn.ava.ymcc.vo.Courseinfo2OrderinfoVo;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/killCourse")
public class KillCourseController {

    @Autowired
    public IKillCourseService killCourseService;

    /**
     * 查询出redis当前所有发布的秒杀商品,渲染到首页
     * @return
     */
    @RequestMapping(value = "/online/all",method = RequestMethod.GET)
    public JSONResult onlineAll(){
        return JSONResult.success(killCourseService.onlineAll());
    }

    /**
     * 秒杀商品详情数据渲染
     * @param killId
     * @param activityId
     * @return
     */
    @RequestMapping(value = "/online/one/{killId}/{activityId}",method = RequestMethod.GET)
    public JSONResult onlineOne(@PathVariable("killId") @NotNull(message = "参数异常!") Long killId, @PathVariable("activityId") @NotNull(message = "参数异常!") Long activityId){
        KillCourse killCourse = killCourseService.onlineOne(killId,activityId);
        return JSONResult.success(killCourse);
    }


    /**
     * 秒杀主接口
     * @param dto
     * @return
     */
    @RequestMapping(value="/kill",method= RequestMethod.POST)
    public JSONResult kill(@RequestBody @Valid KillCourseOrderParamDto dto){
        return JSONResult.success(killCourseService.kill(dto));
    }

    @RequestMapping(value = "/online/oneByOrder/{orderNo}",method = RequestMethod.GET)
    public JSONResult oneByOrder(@PathVariable("orderNo") @NotEmpty(message = "参数异常!") String orderNo){
        Courseinfo2OrderinfoVo orderinfoVo =  killCourseService.oneByOrder(orderNo);
        return JSONResult.success(orderinfoVo);
    }


    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/add",method= RequestMethod.POST)
    public JSONResult addCourseKill(@RequestBody @Valid KillCourse killCourse){
        killCourseService.addCourseKill(killCourse);
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        killCourseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(killCourseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(killCourseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody KillCourseQuery query){
        Page<KillCourse> page = new Page<KillCourse>(query.getPage(),query.getRows());
        page = killCourseService.selectPage(page);
        return JSONResult.success(new PageList<KillCourse>(page.getTotal(),page.getRecords()));
    }
}
